<?php declare(strict_types=1);


namespace App\Service;


class EncryptService
{
    public const PUBLIC_KEY = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCuUwkarUXb0kPisrVD8nk10/t8
Of4EuZ//laUF4jqWL7H88jBFOoGFz2L3CcD5IoTtpihfLh312k2Fs3s1fhzbZxQu
5NXNe/fDA2GlFI/PkZcCDT9gm3XFP2SsOhWICHuytsKtjg2wJ2Ifz1J4qnVuCiTT
SmvkzWvDpZOVr3vDpwIDAQAB
-----END PUBLIC KEY-----';

    public const PRIVATE_KEY = '-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCuUwkarUXb0kPisrVD8nk10/t8Of4EuZ//laUF4jqWL7H88jBF
OoGFz2L3CcD5IoTtpihfLh312k2Fs3s1fhzbZxQu5NXNe/fDA2GlFI/PkZcCDT9g
m3XFP2SsOhWICHuytsKtjg2wJ2Ifz1J4qnVuCiTTSmvkzWvDpZOVr3vDpwIDAQAB
AoGAPqGCsdzzLmk7DqXaTlE9j0NcJWPIifYPMAqZwaVoGzGDpzd0dEry4hqCmfMd
K+VlEOUTuC6fgQcvtwunBq8KuolbWE62/JeJ1e4FBICzRpNNB9COvuC3ktghbiWr
DI0rOxkuHBKS4WchnqSt3EBl3nsTPRkpHFC8pjOes4/hQXECQQDWi5xeeCfA8LRy
n2fukr7j81dLZCFkKNSn3vQFn+wJOdjkqFMh1/o0p6VHKlzCps2DG7wQxH1FqdXM
auhxbMQDAkEA0AHpSRNEMzSpR8X5J+eLuZ/65A4BUJWuNWhLxihN/yEOly75rFii
B6hNc7CA0df9TT3BIjuOegaygQ+5BY6ajQJAYK/0XYKq78HKoKmELjlF+/brjVdn
0Pqxs6G/uVU0C4hdXdcZ9/Ocl+RBkQvfV/3SOlbib3ANwLpkVsaLUKqivwJBAIMw
kWfrZNo+3n1MGabXXMLIab6oLqAap3M7wn7KMNc2jDNCB2pxxE+8EI1kxvqSlUnA
Jy1mDqhbzP2lYvZkSIkCQQDIWVKt3Hc0gT0nsu1JOF/p3sJ9a5OE/cx/6TX2YSry
VJDQlCD6GHb00ENdQ98MBeQkJrOYPMn3rEnn9xk+BrLS
-----END RSA PRIVATE KEY-----';

    /**
     * 私钥解密
     * @param string $encryptString
     * @return string
     */
    public function RSADecrypt($encryptString = ''): string
    {
        if($encryptString === '') {
            return '';
        }
        $decrypted  = '';
        $privateKey = self::PRIVATE_KEY;
        openssl_private_decrypt(base64_decode($encryptString), $decrypted, $privateKey);
        return $decrypted;
    }

    /**
     * 公钥加密
     * @param string $data
     * @return string
     */
    public function publicEncrypt(string $data = ''): string
    {
        $encrypted = '';
        openssl_public_encrypt($data,$encrypted, self::PUBLIC_KEY);
        return base64_encode($encrypted);
    }

}